import datetime

from PyQt4 import QtGui, QtCore

from calendar_table_widget import CalTable

class CalendarWidget(QtGui.QDialog):

    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        lang = parent.lang
        self._db = parent.db
        self.notif = parent.notif
        self.setWindowTitle('PyTv ' + lang['calendar'])
        self.setWindowIcon(QtGui.QIcon(parent.pytv.images['pytv']))
        v_box = QtGui.QVBoxLayout(self)
        h_box = QtGui.QHBoxLayout()
        self._cal = QtGui.QCalendarWidget()
        self._seriesList = QtGui.QListWidget()
        self._seriesList.setVisible(False)
        self._load_series_list()
        h_box.addWidget(self._cal)
        h_box.addWidget(self._seriesList)
        self._complete_calendar_data()
        v_box.addLayout(h_box)
        h_box2 = QtGui.QHBoxLayout()
        self._radioMonth = QtGui.QRadioButton(lang['byMonth'])
        self._radioMonth.setChecked(True)
        self._radioShow = QtGui.QRadioButton(lang['byShow'])
        h_box2.addWidget(self._radioMonth)
        h_box2.addWidget(self._radioShow)
        v_box.addLayout(h_box2)

        self.connect(self._cal, QtCore.SIGNAL("selectionChanged()"), self._check_day)
        self.connect(self._cal, QtCore.SIGNAL("currentPageChanged(int, int)"), self._complete_calendar_data)
        self.connect(self._radioMonth, QtCore.SIGNAL("clicked()"), self._seriesList.hide)
        self.connect(self._radioMonth, QtCore.SIGNAL("clicked()"), self._cal.show)
        self.connect(self._radioShow, QtCore.SIGNAL("clicked()"), self._cal.hide)
        self.connect(self._radioShow, QtCore.SIGNAL("clicked()"), self._seriesList.show)
        self.connect(self._seriesList, QtCore.SIGNAL("itemSelectionChanged()"), self._list_clicked)

    def _complete_calendar_data(self):
        qdate = self._cal.selectedDate()
        month = self._cal.monthShown()
        calendarList = self._db.restore_calendar_for_month(qdate.year(), month)
        for c in calendarList:
            d = QtCore.QDate(c.date.year, c.date.month, c.date.day)
            format = self._cal.dateTextFormat(d)
            format.setFontUnderline(True)
            brush = QtGui.QBrush(QtGui.QColor(47, 79, 79), QtCore.Qt.SolidPattern)
            format.setBackground(brush)
            if c.serie.upper() not in str(format.toolTip()):
                format.setToolTip(format.toolTip() + c.serie.upper() + '\n')
            self._cal.setDateTextFormat(d, format)
            
    def _load_series_list(self):
        series = self._db.restore_series()
        self._seriesList.clear()
        for s in series:
            self._seriesList.addItem(str(s))
            
    def _list_clicked(self):
        try:
            serie = str(self._seriesList.item(self._seriesList.currentRow()).text())
            table = CalTable(self, self._db, byMonth=False, serie=serie)
            table.show()
        except Exception, reason:
            print reason

    def _check_day(self):
        qdate = self._cal.selectedDate()
        date = datetime.date(qdate.year(), qdate.month(), qdate.day())
        table = CalTable(self, self._db, date)
        table.show()

    def closeEvent(self, event):
        event.ignore()
        self.done(0)
